home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 October / EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso / Aminet / comm / net / amitcp2_x_gcc.lha / RCS.RCSfiles / autoinitd.c,v < prev    next >
Text File  |  1994-01-12  |  5KB  |  223 lines

  1. head    1.2;
  2. access;
  3. symbols;
  4. locks
  5.     jasegler:1.2; strict;
  6. comment    @ * @;
  7.  
  8.  
  9. 1.2
  10. date    94.01.12.18.40.51;    author jasegler;    state Exp;
  11. branches;
  12. next    1.1;
  13.  
  14. 1.1
  15. date    94.01.12.18.12.04;    author jasegler;    state Exp;
  16. branches;
  17. next    ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @*** empty log message ***
  27. @
  28. text
  29. @char RCS_ID_AUTOINITD_C[] = "$Id: autoinitd.c,v 1.1 1994/01/12 18:12:04 jasegler Exp jasegler $";
  30. /*
  31.  * SAS C Autoinitialization Functions for Daemons
  32.  *
  33.  * Author: ppessi <Pekka.Pessi@@hut.fi>
  34.  *
  35.  * Copyright © 1993 AmiTCP/IP Group, <amitcp-group@@hut.fi>
  36.  *                  Helsinki University of Technology, Finland.
  37.  *                  All rights reserved.
  38.  *
  39.  * Created      : Mon May 24 19:30:06 1993 ppessi
  40.  * Last modified: Mon Jul 19 14:24:30 1993 jraja
  41.  */
  42.  
  43. /****** inetdlib.doc/autoinitd **********************************************
  44.  
  45.  *   NAME
  46.  *      autoinitd - SAS C Autoinitialization Functions for Daemons
  47.  *
  48.  *   SYNOPSIS
  49.  *      void _STIopenSockets(void);
  50.  *      void _STDcloseSockets(void);
  51.  *      long server_socket;
  52.  *
  53.  *   DESCRIPTION
  54.  *      These are SASC autoinitialization functions for internet daemons
  55.  *      started by inetd, Internet super-server. Upon startup, the server
  56.  *      socket is obtained with ObtainSocket() library call. If successful,
  57.  *      the socket id is stored to the global variable server_socket. If the
  58.  *      socket is not obtainable, the server_socket contains value -1.
  59.  *      If the server_socket is not valid, the server may try to accept() a
  60.  *      new connection and act as a stand-alone server.
  61.  *
  62.  *   RESULT
  63.  *      server_socket - positive socket id for success or -1 for failure.
  64.  *
  65.  *   NOTES
  66.  *      _STIopenSockets() also checks that the system version is at
  67.  *      least 37. It puts up a requester if the bsdsocket.library
  68.  *      is not found or is of wrong version.
  69.  *
  70.  *      The autoinitialization and autotermination functions are
  71.  *      features specific to the SAS C6. However, these functions
  72.  *      can be used with other (ANSI) C compilers, too. Example
  73.  *      follows:
  74.  *
  75.  *      \* at start of main() *\
  76.  *
  77.  *      atexit(_STDcloseSockets);
  78.  *      _STDopenSockets();
  79.  *
  80.  *   AUTHOR
  81.  *      Jarno Rajahalme, Pekka Pessi,
  82.  *      the AmiTCP/IP Group <amitcp-group@@hut.fi>,
  83.  *      Helsinki University of Technology, Finland.
  84.  *
  85.  *   SEE ALSO
  86.  *      serveraccept(), netutil/inetd
  87.  *
  88.  *****************************************************************************
  89.  *
  90.  */
  91.  
  92. #include <exec/types.h>
  93. #include <exec/libraries.h>
  94. #include <dos/dosextens.h>
  95.  
  96. #include <intuition/intuition.h>
  97.  
  98. #ifdef __GNUC__
  99. #include <clib/socket_protos.h>
  100. #include <clib/exec_protos.h>
  101. #include <clib/intuition_protos.h>
  102. #endif
  103.  
  104. #ifdef _DCC
  105. #include <clib/socket_protos.h>
  106. #include <clib/exec_protos.h>
  107. #include <clib/intuition_protos.h>
  108. #endif
  109.  
  110. #ifdef __SASC__
  111. #include <proto/socket.h>
  112. #include <proto/exec.h>
  113. #include <proto/intuition.h>
  114. #endif
  115.  
  116. #include <stdlib.h>
  117.  
  118. #include <inetd.h>
  119.  
  120. struct Library *SocketBase = 0L;
  121.  
  122. int errno = 0;
  123.  
  124. long server_socket = -1;
  125.  
  126. #ifndef SOCKETNAME
  127. #define SOCKETNAME "bsdsocket.library"
  128. #endif
  129.  
  130. #define SOCKETVERSION 2        /* minimum bsdsocket version to use */
  131.  
  132. #ifdef __SASC__
  133. extern STRPTR _ProgramName;    /* SAS startup module defines this :-) */
  134. #endif
  135.  
  136. #ifdef __GNUC__
  137. char _ProgramName[] = "GNU C Program";
  138. #endif
  139.  
  140. #ifdef _DCC
  141. char _ProgramName[] = "DCC Program";
  142. #endif
  143.  
  144. void
  145. _STIopenSockets (void)
  146. {
  147.   struct Process *me = (struct Process *) FindTask (0L);
  148.   struct DaemonMessage *dm = (struct DaemonMessage *) me->pr_ExitData;
  149.   static short done = 0;
  150.   struct Library *IntuitionBase;
  151.   STRPTR errorStr;
  152.  
  153.   if (done++)            /* try only once (SAS/C 6.2 liked to call this twice) */
  154.     return;
  155.  
  156.   /*
  157.    * Check OS version
  158.    */
  159.   if ((*(struct Library **) 4)->lib_Version < 37)
  160.     exit (20);
  161.  
  162.   /*
  163.    * Open bsdsocket.library
  164.    */
  165.   if ((SocketBase = OpenLibrary (SOCKETNAME, SOCKETVERSION)) != NULL)
  166.     {
  167.       /*
  168.          * Succesfull. Now tell bsdsocket.library the address of our errno
  169.        */
  170.       SetErrnoPtr (&errno, sizeof (errno));
  171.  
  172.       if (dm && server_socket == -1)
  173.     {
  174.       server_socket = ObtainSocket (dm->dm_Id, dm->dm_Family, dm->dm_Type, 0);
  175.       if (server_socket == -1)
  176.         exit (DERR_OBTAIN);
  177.     }
  178.       return;
  179.     }
  180.   else
  181.     errorStr = "AmiTCP/IP version 2 or later must be started first.";
  182.  
  183.   IntuitionBase = OpenLibrary ("intuition.library", 36);
  184.  
  185.   if (IntuitionBase != NULL)
  186.     {
  187.       struct EasyStruct libraryES;
  188.  
  189.       libraryES.es_StructSize = sizeof (libraryES);
  190.       libraryES.es_Flags = 0;
  191.       libraryES.es_Title = _ProgramName;
  192.       libraryES.es_TextFormat = errorStr;
  193.       libraryES.es_GadgetFormat = "Exit %s";
  194.  
  195.       EasyRequestArgs (NULL, &libraryES, NULL, (APTR) & _ProgramName);
  196.  
  197.       CloseLibrary (IntuitionBase);
  198.     }
  199.   exit (DERR_LIB);
  200. }
  201.  
  202. void
  203. _STDcloseSockets (void)
  204. {
  205.   if (SocketBase)
  206.     {
  207.       CloseLibrary (SocketBase);
  208.       SocketBase = NULL;
  209.     }
  210. }
  211. @
  212.  
  213.  
  214. 1.1
  215. log
  216. @Initial revision
  217. @
  218. text
  219. @d1 1
  220. a1 1
  221. char RCS_ID_C[] = "$Id: autoinitd.c,v 1.4 1993/07/19 11:24:40 jraja Exp $";
  222. @
  223.